<?php
/**
 * 素材管理
 */

namespace backend\controllers;

use yii;
use common\models\Article;

class ArticleController extends CommonController
{
    public function actions()
    {
        return [
            'Kupload' => [
                'class' => 'pjkui\kindeditor\KindEditorAction'
            ]
        ];
    }

    public function actionIndex()
    {
        $this->setQuery(Article::find()->orderBy(['id' => SORT_DESC]));
        return $this->query();
    }

    public function actionCreate()
    {
        $model = new Article();
        $this->setModel($model);
        $this->setParams(['p1' => [], 'p2' => []]);
        return $this->save();
    }

    public function actionUpdate($id)
    {
        $model = Article::findOne($id);
        $p1 = $p2 = [];
        if ($model->photos) {
            $photos = explode('|', $model->photos);
            foreach ($photos as $photo) {
                $p1[] = Yii::getAlias('@frontendUrl') . $photo;
                $p2[] = ['url' => yii\helpers\Url::to(['upload-photos']), 'key' => $photo];
            }
        }
        $this->setModel($model);
        $this->setParams(['p1' => $p1, 'p2' => $p2]);
        return $this->save();
    }

    public function actionDelete()
    {
        $this->setModel(new Article());
        $this->delete();
    }


}